<?php

class Admin extends Admin_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Admin_m');
        $this->_data['erreur'] = "";
    }

    function index() {
        is_admin();
        $this->list_admin();
    }

    function login() {
        $config = array(
            array(
                'field' => 'username',
                'label' => 'username',
                'rules' => 'required'
            ),
            array(
                'field' => 'password',
                'label' => 'password',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $username = $this->input->post('username');
                $password = md5($this->input->post('password'));

                $query = $this->Admin_m->login($username, $password);
                if ($query->num_rows() > 0) {
                    $row = $query->row();
                    $this->session->set_userdata('adminid', $row->id);
                    $this->session->set_userdata('username', $row->username);
                    redirect('admin');
                } else {
                    $this->_data['erreur'] = "Identifiant ou mot de passe n'existe pas";
                    $this->display_view('admin/login');
                }
            } else {
                $this->display_view('admin/login');
            }
        } else {
            $this->display_view('admin/login');
        }
    }

    function logout() {
        $this->session->unset_userdata('adminid');
        redirect(site_url('admin'));
    }

    function list_admin() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 2;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/admin/list_admin' . '/' . $per_page . '/';
        $config['per_page'] = '2';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Admin_m->count_all_user();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Admin_m->get_all_user($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Admin_m->count_all_user();
        $this->_data['page_title'] = "Liste de Admin";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('admin/list_admin');
    }

    function delete_admin($id, $per_page, $off_set) {
        is_super_admin();
        $this->Admin_m->delete_admin($id);
        redirect(site_url('admin/list_admin'));
    }

    function add_admin() {
        is_admin();
        if ($this->input->post('register')) {
            $formVal = array(
                array(
                    'field' => 'username',
                    'Label' => 'User name',
                    'rules' => 'required|callback_check_exist_username|xss_clean'
                ),
                array(
                    'field' => 'pass',
                    'Label' => 'Password',
                    'rules' => 'required|matches[confirm_pass]|min_length[5]'
                ),
                array(
                    'field' => 'confirm_pass',
                    'Label' => 'Confirm Password',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);

            if ($this->form_validation->run() === TRUE) {
                $username = $this->input->post('username');
                $password = $this->input->post('pass');
                $data_user = array('username' => $username,
                    'password' => md5($password)
                );
                $this->Admin_m->add_admin($data_user);

                redirect('admin/list_admin');
            } else {
                $this->display_view('admin/add_admin');
            }
        } else {
            $this->display_view('admin/add_admin');
        }
    }

    function check_exist_username() {
        $username = $this->input->post('username');
        $query = $this->Admin_m->get_exist_username($username);
        if ($query->num_rows() > 0) {
            $this->form_validation->set_message('check_exist_username', 'The user name was exist');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function change_pass($id) {
        is_admin();
        $adminid = $this->session->userdata('adminid');
        $this->_data['erreur'] = "";

        $config = array(
            array(
                'field' => 'apass',
                'label' => 'Ancien mot de passe',
                'rules' => 'required|md5'
            ),
            array(
                'field' => 'npass',
                'label' => 'Nouveau mot de passe',
                'rules' => 'required|matches[confirm_npass]|md5'
            ),
            array(
                'field' => 'confirm_npass',
                'label' => 'Confirmation du mot de passe',
                'rules' => 'required|md5'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run()) {
            $pass = $this->input->post('apass');
            $query = $this->Admin_m->is_password($pass, $adminid);
            if ($query) {
                $data = array('password' => $this->input->post('npass'));
                $query = $this->Admin_m->change_pass($data, $adminid);
                redirect('admin/list_admin');
            } else {
                $this->_data['erreur'] = "Mot de passe incorrect";
                $this->display_view('admin/change_pass');
            }
        }else
            $this->display_view('admin/change_pass');
    }

}

?>
